// pages/logistics/logistics.js
var app = getApp();
const getLogisticsUrl = '/Order/orderExpress';
const viewLogisticsUrl = '/Order/orderExpressInfo';
const reg = /((((13[0-9])|(15[^4])|(18[0,1,2,3,5-9])|(17[0-8])|(147))\d{8})|((\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}))?/g;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: 1, //是否显示加载中图标
    courierPhoneNum: '', //快递员手机号
    tipsText: '', //查询不到物流信息时的提示
    logisticsInfoShow: [], //用来展示的物流信息
    logisticsInfo: [], //物流信息
    page: 1, //商品推荐页码
    recommendList: [], //商品推荐列表
    express_tel: '', //快递公司手机号
    isHasExpress: '', //判断是否获取到了物流信息
    order_id: 0, //商品订单ID
    province_name: '', //一级地址
    city_name: '', //二级地址
    county_name: '', //三级地址
    detail_info: '', //详细地址
    express_company_name: '', //快递公司
    express_number: '', //订单号
    goods_image: '', //商品图片
  },
  // 跳转商品详情页
  goDetail: function(e) {
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 复制订单号
  copyEvent: function(e) {
    wx.setClipboardData({
      data: this.data.express_number
    })
  },
  // 展开或隐藏物流信息
  operationBtn: function(e) {
    if (e.currentTarget.dataset.type == 1) {
      this.setData({
        logisticsInfoShow: this.data.logisticsInfo
      })
    } else {
      this.setData({
        logisticsInfoShow: this.data.logisticsInfo.slice(0, 1)
      })
    }

  },
  // 获取物流信息接口
  getLogistics: function() {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      order_id: _this.data.order_id
    }
    app.post(getLogisticsUrl, requestData, function(res) {
      if(res.code==2000){
      var configs = wx.getStorageSync('configs');
      var data = res.data;
      _this.setData({
        isLoading: 0,
        province_name: data.province_name,
        city_name: data.city_name,
        county_name: data.county_name,
        detail_info: data.detail_info,
        express_company_name: data.express_company_name,
        express_number: data.express_number,
        goods_image: configs.static_url_http + '/' + data.goods_image,
        express_tel: data.tel
      })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 查看物流信息接口
  viewLogistics: function() {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      order_id: _this.data.order_id
    }
    app.post(viewLogisticsUrl, requestData, function(res) {
        var configs = wx.getStorageSync('configs');
        if (res.message != 'ok') {
          _this.setData({
            isHasExpress: false,
            tipsText: res.message
          })
        } else {
          var indexList = [];
          var phoneNumList = [];
          var phoneNum;
          for (var j = 0; j < res.data.length; j++) {
            phoneNum = res.data[j].context.match(reg);

            for (var k = 0; k < phoneNum.length; k++) {
              if (phoneNum[k] !== '') {
                phoneNum = phoneNum[k];
                indexList.push(j);
                phoneNumList.push(phoneNum);
              }
            }
          }
          console.log(indexList, phoneNumList);
          var context;
          console.log(res.data);
          for (var a = 0; a < indexList.length; a++) {
            var dataIndex = indexList[a];
            context = res.data[dataIndex].context.split(phoneNumList[a]);
            context.splice(1, 0, phoneNumList[a]);
            res.data[dataIndex].context = context;
            console.log(res.data[dataIndex], dataIndex);
          }
          console.log(res.data);
          var length = context.length;
          if (res.state == 3) {
            res.data[1].context = [context[0], phoneNum[0], context[context.length - 1]]
          }
          _this.setData({
            expressState: res.state,
            courierPhoneNum: phoneNum[1],
            isHasExpress: true,
            logisticsInfo: res.data,
            logisticsInfoShow: res.data.slice(0, 1)
          })
        }
    });
  },
  callPhoneNum: function(e) {
    console.log(e);
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.phone_num
    })
  },
  // 获取商品推荐列表
  getRecommendList: function() {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      search_type: 0,
      page: _this.data.page
    }
    app.post('/Goods/recommend', requestData, function(res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var recommendList = _this.data.recommendList;
        var configs = wx.getStorageSync('configs');
        for (var i = 0; i < res.data.list.length; i++) {
          res.data.list[i].img = configs.static_url_http + '/' + res.data.list[i].img;
        }
        recommendList = recommendList.concat(res.data.list);
        _this.setData({
          page: res.data.page,
          recommendList: recommendList
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      order_id: options.order_id
    });
    this.loop();
  },
  // 判断是否获取到token的值了
  loop: function() {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    var theRegionUnit = _this.data.regionUnit;
    if (!theToken) {
      setTimeout(function() {
        _this.loop()
      }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.viewLogistics();
      _this.getLogistics();
      _this.getRecommendList();
    }
  }
})